<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="" alt="" />
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <el-aside  :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="Collspace" >lll</div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b" 
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
            :default-active="default_active"
          >
            <el-submenu :index="item.id +''" v-for="item in menusList" :key="item.id">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/'+items.path+''" v-for="items in item.children" :key="items.id" @click="saveNavState(items.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{items.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
      </el-aside>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menusList: [],
      isCollapse: false,
      default_active: ''
    }
  },
  created() {
    this.getMenuList()
    this.default_active=sessionStorage.getItem('default_active')
  },
  methods: {
    loginOut() {
      window.sessionStorage.clear()
      this.$router.push("/login")
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("/menus")
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menusList = res.data
      console.log(this.menusList)
    },
    Collspace(){
      this.isCollapse=!this.isCollapse
      console.log(this.isCollapse)
    },
    saveNavState(path){
      this.default_active=path
      sessionStorage.setItem('default_active',path)
    }
  }
}
</script>

<style lang="less" scope>
.home-container {
  height: 100%;
}
.el-header {
  width: 100%;
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 5px;
    }
  }
}
.el-aside {
  // height: 100%;
  background-color: #333744;
  .el-menu{
    border: 0;
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  color: white;
  line-height: 15px;
  letter-spacing: 2px;
  text-align: center;
}
</style>
